package com.kc.business.service.impl;

import com.kc.business.domain.*;
import com.kc.business.mapper.*;
import com.kc.business.service.IBizShopEnterService;
import com.kc.common.constant.BizConstants;
import com.kc.common.constant.UserConstants;
import com.kc.common.core.domain.entity.SysRole;
import com.kc.common.core.domain.entity.SysUser;
import com.kc.common.exception.CustomException;
import com.kc.common.utils.SecurityUtils;
import com.kc.system.domain.SysUserRole;
import com.kc.system.mapper.SysRoleMapper;
import com.kc.system.mapper.SysUserMapper;
import com.kc.system.mapper.SysUserRoleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author zhangshuo
 * @version 1.0
 * @date 2020/9/10
 */
@Service

@Slf4j
public class BizShopEnterServiceImpl implements IBizShopEnterService {


    @Resource
    BizShopEnterMapper bizShopEnterMapper;
    @Resource
    BizUserMapper bizUserMapper;
    @Resource
    BizScenicBusinessMapper bizScenicBusinessMapper;
    @Resource
    SysUserMapper adminMapper;
    @Resource
    SysUserRoleMapper sysUserRoleMapper;
    @Resource
    SysRoleMapper sysRoleMapper;
    @Resource
    BizRoleMapper bizRoleMapper;
    @Resource
    BizUserRoleMapper bizUserRoleMapper;
    @Resource
    BizScenicBusinessTypeMapper bizScenicBusinessTypeMapper;

    @Override
    public List<BizShopEnterForm> getList(BizShopEnterForm bizShopEnterForm) {
        List<BizShopEnterForm> bizShopEnterForms = bizShopEnterMapper.select(bizShopEnterForm);
        for (BizShopEnterForm shopEnterForm : bizShopEnterForms){
            //todo 处理类别名称，后期需要做缓存
            BizScenicBusinessType bizScenicBusinessType = bizScenicBusinessTypeMapper.selectByPrimaryKey(shopEnterForm.getMainTypeId());
            if(bizScenicBusinessType != null) {
                shopEnterForm.setMainTypeName(bizScenicBusinessType.getTypeName());
            }
            BizScenicBusinessType scenicBusinessType = bizScenicBusinessTypeMapper.selectByPrimaryKey(shopEnterForm.getTypeId());

            if(scenicBusinessType != null) {
                shopEnterForm.setTypeName(scenicBusinessType.getTypeName());
            }
        }
        return bizShopEnterForms;
    }

    @Override
    public int add(BizShopEnterForm bizShopEnterForm) {
        return bizShopEnterMapper.insertSelective(bizShopEnterForm);
    }

    @Override
    @Transactional
    public int approval(@Validated BizShopEnterForm bizShopEnterForm) {
        //通过审核创建商家，更新用户商家状态
        if (BizConstants.PASS_VERIFY.equals(bizShopEnterForm.getStatus())) {
            BizShopEnterForm enterForm = bizShopEnterMapper.selectByPrimaryKey(bizShopEnterForm.getEnterId());
            log.info("[商家入驻审核]查询审核数据{}",enterForm);
            //APP用户id
            int useId = enterForm.getUserId();

            //更新APP用户
            BizUser bizUser = bizUserMapper.selectByPrimaryKey(useId);
            bizUser.setId(useId);
            bizUser.setIsBusiness(BizConstants.YES);
            bizUserMapper.updateByPrimaryKeySelective(bizUser);

            //创建商家
            BizScenicBusiness bizScenicBusiness = new BizScenicBusiness();
            bizScenicBusiness.setUserId(useId);
            bizScenicBusiness.setName(enterForm.getShopName());
            bizScenicBusiness.setMainTypeId(enterForm.getMainTypeId());
            bizScenicBusiness.setLatitude(enterForm.getLatitude());
            bizScenicBusiness.setLongitude(enterForm.getLongitude());
            bizScenicBusiness.setAdcode(enterForm.getAdcode());
            bizScenicBusiness.setAddress(enterForm.getShopAddress());
            bizScenicBusiness.setTypeId(enterForm.getTypeId());
            log.info("[商家入驻审核]创建商家数据{}",enterForm);
            bizScenicBusinessMapper.insertSelective(bizScenicBusiness);
            //创建商家PC端账号
            SysUser admin = new SysUser();
            admin.setNickName(enterForm.getName());
            admin.setPhonenumber(bizUser.getMobile());
            admin.setUserName(bizUser.getMobile());
            if (UserConstants.NOT_UNIQUE.equals(adminMapper.checkUserNameUnique(admin.getUserName()) > 0 ? "1" : "0")) {
                admin = adminMapper.selectUserByUserName(admin.getUserName());
            } else {
                //身份证后6位做密码
                String idCard = enterForm.getIdCard();
                String password = idCard.substring(idCard.length() - 6, idCard.length());
                admin.setPassword(SecurityUtils.encryptPassword(password));
                admin.setTenantId(enterForm.getUserId());
                adminMapper.insertUser(admin);

                //查询PC角色
                SysRole sysRole = sysRoleMapper.checkRoleKeyUnique(UserConstants.ROLE_BUSINESS_PC);
                if (sysRole == null) {
                    throw new CustomException("请联系管理员，创建PC角色");
                }
                //设置后台用户角色
                SysUserRole userRole = new SysUserRole();
                userRole.setUserId(admin.getUserId());
                userRole.setRoleId(sysRole.getRoleId());

                // 新增用户与角色管理
                List<SysUserRole> list = new ArrayList<SysUserRole>();
                list.add(userRole);
                sysUserRoleMapper.batchUserRole(list);
            }

            //查询APP角色
            BizRole appRole = bizRoleMapper.getActiveByRoleKey(UserConstants.ROLE_BUSINESS_APP);
            if (appRole == null) {
                throw new CustomException("请联系管理员，创建APP角色");
            }
            //设置APP用户角色
            BizUserRole bizUserRole = new BizUserRole();
            bizUserRole.setUserId(bizUser.getId());
            bizUserRole.setRoleId(appRole.getId());
            if (bizUserRoleMapper.selectOne(bizUserRole) == null) {
                bizUserRoleMapper.insert(bizUserRole);
            }
        }

        return bizShopEnterMapper.updateByPrimaryKeySelective(bizShopEnterForm);
    }
}
